package com.cs.service;

import com.cs.mapper.UserMapper;
import com.cs.pojo.User;
import com.cs.pojo.UserExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    public PageInfo<User> queryAll(String username,int pageNum,int pageSize){
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (username!=null||!username.trim().equals("")){
            criteria.andUTruenameLike("%"+username.trim()+"%");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByExample(example);
        return new PageInfo<>(users);
    }

    public User queryById(int uId){
        User user = userMapper.selectByPrimaryKey(uId);
        return user;
    }

    public int update(User user){
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    public int deleteOne(int uId){
        User user=new User();
        user.setuId(uId);
        user.setuState(2);
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    public List<User> queryLPR(int uLoginname , int rId){
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andULoginnameEqualTo(uLoginname);
        criteria.andRIdEqualTo(rId);
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    public List<User> queryByLoginName(String uLoginname){
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andULoginnameEqualTo(Integer.parseInt(uLoginname));
        List<User> users = userMapper.selectByExample(example);
        return users;

    }

    public int updatePassword(String loginname,String newpass){
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andULoginnameEqualTo(Integer.parseInt(loginname));

        User user=new User();
        user.setuPassword(newpass);
        int i = userMapper.updateByExampleSelective(user, example);
        return i;
    }

    public int add(User user){
        int insert = userMapper.insert(user);
        return insert;
    }

    public List<User> queryAll2(){
        List<User> users = userMapper.selectByExample(null);
        return users;
    }
}
